package com.jiaozi.core.security.controller;

import com.jiaozi.base.redis.RedisUtil;
import com.jiaozi.base.utils.bean.R;
import com.jiaozi.base.utils.bean.enums.REnum;
import com.jiaozi.core.security.pojo.LoginUserDetails;
import com.jiaozi.core.security.pojo.param.LoginParam;
import com.jiaozi.core.security.service.ILoginService;
import com.jiaozi.core.security.utils.SecurityUtil;
import com.jiaozi.system.enums.ConfigsEnum;
import com.jiaozi.system.service.SysConfigsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 登录接口
 */
@Tag(name = "登录")
@RestController
@Slf4j
public class LoginController {

    @Autowired
    private ILoginService iLoginService;
    @Autowired
    private SysConfigsService sysConfigsService;
    @Autowired
    private RedisUtil redisUtil;

    @Operation(summary = "系统登录",description = "系统登录")
    @PostMapping("/login")
    public R login(@RequestBody @Valid LoginParam loginParam){

        R r=iLoginService.login(loginParam);
        if(r.getCode().equals(REnum.SUCCESS.getCode())&&loginParam.getPassword().equals(sysConfigsService.getSysConfigs(ConfigsEnum.LOGIN_DEFAULT_PASSWORD))){
            r.setCode(REnum.INITIALIZE_PASSWORD.getCode());
            r.setMessage(REnum.INITIALIZE_PASSWORD.getMsg());
        }
        return r;
    }



    @Operation(summary = "获取登录用户信息",description = "获取登录用户信息")
    @GetMapping("/getLoginUserInfo")
    public  R<LoginUserDetails> getLoginUserInfo(){
        return R.ok(SecurityUtil.getLoginUser());
    }


}
